package me.mingshan.leetcode;

/**
 * @author hanjuntao
 * @date 2025/6/18 0018
 */
public class UL_219_contains_duplicate_ii {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 1};
        System.out.println(containsNearbyDuplicate(nums, 3));
    }

    /**
     * 往后找k个数，判断是否存在
     *
     * 时间复杂度：O(nk)
     * 空间复杂度：O(1)
     *
     * 超时-----------------
     *
     * @param nums
     * @param k
     * @return
     */
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j <= j + k && j < nums.length; j++) {
                if (i != j && nums[i] == nums[j] && Math.abs(i - j) <= k) {
                    return true;
                }
            }
        }

        return false;
    }
}
